const fs = require("fs");
const path = require("path");

// 生成writeStream
function createWriteStream(fileName) {
  const fullFileName = path.join(__dirname, fileName);
  const writeStream = fs.createWriteStream(fullFileName, {
    flags: "a", //'a'为追加，'w'为覆盖
  });
  return writeStream;
}

const writeStream = createWriteStream("debug.log");
const formater = new Intl.DateTimeFormat("zh-cn", {
  year: "numeric",
  month: "2-digit",
  day: "2-digit",
  hour: "2-digit",
  minute: "2-digit",
  second: "2-digit",
});

function log() {
  let logs = [...arguments].join(",");
  let fullLog = `${formater.format(new Date())} [${process.pid}]: ${logs}\n`;
  console.log(fullLog);
  writeStream.write(fullLog);
}

module.exports = {
  log,
};
